----------------------------------------------------------------
-- Global Variables
----------------------------------------------------------------

EA_Window_CityTracker = {}

EA_Window_CityTracker.EMPTY_SIZE = { x=600, y=100 } -- Used to provide a size guide for the layout editor.

EA_Window_CityTracker.NUM_OBJECTIVES = 1
EA_Window_CityTracker.NUM_QUESTS = 2
EA_Window_CityTracker.NUM_CONDITION_COUNTERS = 5

EA_Window_CityTracker.WIDTH = 600
EA_Window_CityTracker.CONDITION_NAME_WIDTH = 530
EA_Window_CityTracker.CAPTURE_BAR_HEIGHT   = 50

EA_Window_CityTracker.currentData = {}
EA_Window_CityTracker.Strings     =
{
    cityName = L"",
    attackerRealmName = L"",
    defenderRealmName = L"",
    sideSuffix = "",
    PQ1 = L"",
    PQ2 = L"",
}

EA_Window_CityTracker.mainObjective = 0


----------------------------------------------------------------
-- Local Variables
----------------------------------------------------------------

local CompleteCounterColor      = DefaultColor.TEAL     -- Not used; instead, we use the realm's color (red or blue) to mean complete
local IncompleteCounterColor    = DefaultColor.WHITE
local CompleteQuestTitleColor   = CompleteCounterColor 
local IncompleteQuestTitleColor = IncompleteCounterColor

----------------------------------------------------------------
-- Local functions
----------------------------------------------------------------
local function DisplayObjectiveOutOfRangeError(index)
    if (index)
    then
        ERROR(L"Active objective #"..index..L" updated, the City Tracker only supports one objective with one quest")
    else
        ERROR(L"nil active objective updated, what's that mean?")
    end
end
----------------------------------------------------------------
-- EA_Window_CityTracker Functions
----------------------------------------------------------------
-- OnInitialize Handler
function EA_Window_CityTracker.Initialize()

    LayoutEditor.RegisterWindow( "EA_Window_CityTracker",
                                GetStringFromTable( "HUDStrings", StringTables.HUD.LABEL_HUD_EDIT_CITY_TRACKER_WINDOW_NAME ),
                                GetStringFromTable( "HUDStrings", StringTables.HUD.LABEL_HUD_EDIT_CITY_TRACKER_WINDOW_DESC ),
                                false, false,
                                false, nil,
                                { "topleft", "top", "topright" } )
                                

    WindowRegisterEventHandler( "EA_Window_CityTracker", SystemData.Events.PUBLIC_QUEST_ADDED,   "EA_Window_CityTracker.OnQuestAdded")
    WindowRegisterEventHandler( "EA_Window_CityTracker", SystemData.Events.PUBLIC_QUEST_UPDATED, "EA_Window_CityTracker.OnQuestUpdated")
    WindowRegisterEventHandler( "EA_Window_CityTracker", SystemData.Events.PUBLIC_QUEST_REMOVED, "EA_Window_CityTracker.OnQuestRemoved")
    WindowRegisterEventHandler( "EA_Window_CityTracker", SystemData.Events.PUBLIC_QUEST_CONDITION_UPDATED, "EA_Window_CityTracker.OnQuestConditionUpdated")
    WindowRegisterEventHandler( "EA_Window_CityTracker", SystemData.Events.PUBLIC_QUEST_COMPLETED, "EA_Window_CityTracker.OnQuestCompleted")
    WindowRegisterEventHandler( "EA_Window_CityTracker", SystemData.Events.PUBLIC_QUEST_FAILED, "EA_Window_CityTracker.OnQuestFailed")
    WindowRegisterEventHandler( "EA_Window_CityTracker", SystemData.Events.PUBLIC_QUEST_RESETTING, "EA_Window_CityTracker.OnQuestResetting")
    WindowRegisterEventHandler( "EA_Window_CityTracker", SystemData.Events.PUBLIC_QUEST_OPTOUT, "EA_Window_CityTracker.OnQuestOptOut")

    WindowRegisterEventHandler( "EA_Window_CityTracker", SystemData.Events.PUBLIC_QUEST_CHANGED_STATE,   "EA_Window_CityTracker.OnQuestStateChanged")

    WindowRegisterEventHandler( "EA_Window_CityTracker", SystemData.Events.OBJECTIVE_CONTROL_POINTS_UPDATED, "EA_Window_CityTracker.UpdateControl" )
    
    WindowRegisterEventHandler( "EA_Window_CityTracker", SystemData.Events.CITY_SCENARIO_BEGIN,          "EA_Window_CityTracker.Refresh")
    WindowRegisterEventHandler( "EA_Window_CityTracker", SystemData.Events.CITY_SCENARIO_UPDATE_STATUS,  "EA_Window_CityTracker.UpdateStatus")
    WindowRegisterEventHandler( "EA_Window_CityTracker", SystemData.Events.CITY_SCENARIO_UPDATE_TIME,    "EA_Window_CityTracker.UpdateStatus")
    WindowRegisterEventHandler( "EA_Window_CityTracker", SystemData.Events.CITY_SCENARIO_END,            "EA_Window_CityTracker.Refresh")

    WindowRegisterEventHandler( "EA_Window_CityTracker", SystemData.Events.LOADING_END,                  "EA_Window_CityTracker.Start" )
    WindowRegisterEventHandler( "EA_Window_CityTracker", SystemData.Events.INTERFACE_RELOADED,           "EA_Window_CityTracker.Start" )
    WindowRegisterEventHandler( "EA_Window_CityTracker", SystemData.Events.PLAYER_ZONE_CHANGED,          "EA_Window_CityTracker.Refresh" )
    
    StatusBarSetMaximumValue( "EA_Window_CityTrackerCaptureControlBar", 100 )
        
    EA_Window_CityTracker.Refresh()
end

function EA_Window_CityTracker.InitializeLayout()
    EA_Window_CityTracker.UpdateMainWindowSize()
end

-- OnUpdate Handler
function EA_Window_CityTracker.Update( timePassed )
    -- Update the timers
    EA_Window_CityTracker.UpdateQuestTimer()
end

-- OnShutdown Handler
function EA_Window_CityTracker.Shutdown()

end

function EA_Window_CityTracker.Refresh()
    WindowSetShowing("EA_Window_CityTracker", GameData.Player.isInSiege)
    EA_Window_CityTracker.UpdateStatus()
    EA_Window_CityTracker.UpdateQuestVisibility()
    EA_Window_CityTracker.UpdateTracker()
end

function EA_Window_CityTracker.Start()
    EA_Window_CityTracker.Strings.cityName = GetZoneName(GameData.Player.zone)
    EA_Window_CityTracker.Strings.attackerRealmName = L""
    EA_Window_CityTracker.Strings.defenderRealmName = L""
    EA_Window_CityTracker.Strings.sideSuffix = "_ATTACK"
    EA_Window_CityTracker.Strings.PQ1 = L""
    EA_Window_CityTracker.Strings.PQ2 = L""

    if (GameData.Player.isInSiege)
    then

        if GameData.CityScenarioData.playerIsDefending
        then
            EA_Window_CityTracker.Strings.sideSuffix = "_DEFEND"
            
            if (GameData.Player.realm == GameData.Realm.ORDER)
            then
                EA_Window_CityTracker.Strings.defenderRealmName = GetStringFromTable("WorldControl", StringTables.WorldControl.LABEL_REALM_ORDER)
                EA_Window_CityTracker.Strings.attackerRealmName = GetStringFromTable("WorldControl", StringTables.WorldControl.LABEL_REALM_DESTRUCTION)
            else
                EA_Window_CityTracker.Strings.defenderRealmName = GetStringFromTable("WorldControl", StringTables.WorldControl.LABEL_REALM_DESTRUCTION)
                EA_Window_CityTracker.Strings.attackerRealmName = GetStringFromTable("WorldControl", StringTables.WorldControl.LABEL_REALM_ORDER)
            end
        else
            if (GameData.Player.realm == GameData.Realm.ORDER)
            then
                EA_Window_CityTracker.Strings.defenderRealmName = GetStringFromTable("WorldControl", StringTables.WorldControl.LABEL_REALM_DESTRUCTION)
                EA_Window_CityTracker.Strings.attackerRealmName = GetStringFromTable("WorldControl", StringTables.WorldControl.LABEL_REALM_ORDER)
            else
                EA_Window_CityTracker.Strings.defenderRealmName = GetStringFromTable("WorldControl", StringTables.WorldControl.LABEL_REALM_ORDER)
                EA_Window_CityTracker.Strings.attackerRealmName = GetStringFromTable("WorldControl", StringTables.WorldControl.LABEL_REALM_DESTRUCTION)
            end
        end
        
        -- Extract the two subboss PQ names.
        local objectiveList = GetGameDataObjectives()
        for _, objectiveData in ipairs(objectiveList)
        do
            if objectiveData.isCityBoss
            then
                if EA_Window_CityTracker.Strings.PQ1 == L""
                then
                    EA_Window_CityTracker.Strings.PQ1 = objectiveData.name
                elseif EA_Window_CityTracker.Strings.PQ2 == L""
                then
                    EA_Window_CityTracker.Strings.PQ2 = objectiveData.name
                    break
                else
                    -- Shouldn't be possible.
                    ERROR(L"More than two city sub bosses detected")
                    break
                end
            end
        end
        
        EA_Window_CityTracker.Refresh()
    end
end


----------------------------------------------------------------
-- Quests
----------------------------------------------------------------
function EA_Window_CityTracker.OnQuestAdded()
    -- DEBUG(L"EA_Window_CityTracker.OnQuestAdded()")

    DataUtils.activeObjectivesData = GetActiveObjectivesData()
    local index = GameData.ActiveObjectives.updatedObjectiveIndex
    
    -- DEBUG(L"EA_Window_CityTracker.OnQuestAdded: index="..index )
    -- DUMP_TABLE(DataUtils.activeObjectivesData[index])

    -- Add the data
    EA_Window_CityTracker.currentData[index] = { objectiveId=DataUtils.activeObjectivesData[index].id }
    
    -- Update the Window to Include the new objective       
    EA_Window_CityTracker.UpdateQuestVisibility()
    EA_Window_CityTracker.UpdateTracker()
    EA_Window_CityTracker.UpdateMainWindowSize()
    
    -- Sound
    Sound.Play( Sound.PUBLIC_QUEST_ADDED )
    
end

function EA_Window_CityTracker.OnQuestResetting()
    DataUtils.activeObjectivesData = GetActiveObjectivesData()
    -- DEBUG(L"EA_Window_CityTracker.OnQuestResetting" )

    local index = GameData.ActiveObjectives.updatedObjectiveIndex

    -- doesn't occur in cities?    
end

function EA_Window_CityTracker.OnQuestUpdated()
    DataUtils.activeObjectivesData = GetActiveObjectivesData()
    -- DEBUG(L"EA_Window_CityTracker.OnQuestUpdated" )

    local index = GameData.ActiveObjectives.updatedObjectiveIndex

    -- Update the Window to Include the new objective       
    EA_Window_CityTracker.UpdateTracker()
    EA_Window_CityTracker.UpdateMainWindowSize()
end


function EA_Window_CityTracker.OnQuestRemoved()

    DataUtils.activeObjectivesData = GetActiveObjectivesData()
    
    local index = GameData.ActiveObjectives.updatedObjectiveIndex
    -- DEBUG(L"EA_Window_CityTracker.OnQuestRemoved() index = "..index)

    -- Slide all of the other data up on the window
    for objective = index, EA_Window_CityTracker.NUM_OBJECTIVES - 1
    do
        EA_Window_CityTracker.currentData[objective] = EA_Window_CityTracker.currentData[objective+1]
    end
    EA_Window_CityTracker.currentData[EA_Window_CityTracker.NUM_OBJECTIVES] = nil
    
    -- Update the windows
    EA_Window_CityTracker.UpdateQuestVisibility()
    EA_Window_CityTracker.UpdateTracker()
    EA_Window_CityTracker.UpdateMainWindowSize()
    
end

function EA_Window_CityTracker.OnQuestCompleted()
    -- DEBUG(L"EA_Window_CityTracker.OnQuestCompleted()")
    DataUtils.activeObjectivesData = GetActiveObjectivesData()
    -- Sound
    Sound.Play( Sound.PUBLIC_QUEST_COMPLETED )
end

function EA_Window_CityTracker.OnQuestFailed()
    -- DEBUG(L" **EA_Window_CityTracker.OnQuestFailed" )
    DataUtils.activeObjectivesData = GetActiveObjectivesData()
    -- Sound
    Sound.Play( Sound.PUBLIC_QUEST_FAILED )
end

function EA_Window_CityTracker.OnQuestConditionUpdated()

    -- DEBUG(L" **EA_Window_CityTracker.OnQuestConditionUpdated" )
    DataUtils.activeObjectivesData = GetActiveObjectivesData()
    
    local objective  = GameData.ActiveObjectives.updatedObjectiveIndex
    local questIndex = GameData.ActiveObjectives.updatedQuestIndex
    local condition  = GameData.ActiveObjectives.updatedQuestConditionIndex

    local objectiveData = DataUtils.activeObjectivesData[objective]
    local questData     = objectiveData.Quest[questIndex]
    
    if (objective == EA_Window_CityTracker.mainObjective)
    then
        local questData = objectiveData.Quest[questIndex]

        -- Update only the Condtion Counters
        local questWindowString = "EA_Window_CityTrackerMainQuest"..questIndex
        
        for index, data in pairs(questData.conditions)
        do
            local conditionName = data.name
            local curCounter    = data.curCounter
            local maxCounter    = data.maxCounter
                    
            local counterLabel = questWindowString.."DataCondition"..index.."Counter"
            local counterName  = questWindowString.."DataCondition"..index.."Name"

            -- Only show the conditions if conditionName is not empty
            if( conditionName ~= L"" )
            then
            -- DEBUG(L"Made it through 06 - conditions")
                      
                if( maxCounter > 0 )
                then
                    LabelSetText( counterLabel, L""..curCounter..L"/"..maxCounter )

                    if( curCounter == maxCounter)
                    then
                        local completeColorForRealm = DefaultColor.RealmColors[questData.availableRealm]
                        LabelSetTextColor(counterLabel, completeColorForRealm.r, completeColorForRealm.g, completeColorForRealm.b )
                        LabelSetTextColor(counterName,  completeColorForRealm.r, completeColorForRealm.g, completeColorForRealm.b )
                    else
                        LabelSetTextColor(counterLabel, IncompleteCounterColor.r, IncompleteCounterColor.g, IncompleteCounterColor.b)
                        LabelSetTextColor(counterName,  IncompleteCounterColor.r, IncompleteCounterColor.g, IncompleteCounterColor.b)
                    end         
                    
                    --DEBUG(L"    Condition #"..condition..L": "..curCounter..L" of "..maxCounter..L" counters.")
                else
                    LabelSetText( counterLabel, L"" )
                end
                
            end   
        end
    end
    
end


function EA_Window_CityTracker.OnQuestStateChanged()
    DataUtils.activeObjectivesData = GetActiveObjectivesData()
    -- DEBUG(L" EA_Window_CityTracker.OnQuestStateChanged" )

    local index = GameData.ActiveObjectives.updatedObjectiveIndex

    -- Update the Quest Data 
    EA_Window_CityTracker.UpdateTracker()
    EA_Window_CityTracker.UpdateMainWindowSize()
end


function EA_Window_CityTracker.UpdateQuestTimer()
    local overallTimerString = "EA_Window_CityTrackerOverview"
    
    -- Timer (only show timer at all if the time left is positive)
    if( GameData.CityScenarioData.timeLeft <= 0 )
    then
        LabelSetText( overallTimerString.."TimerValue", L"" )      
        WindowSetShowing( overallTimerString.."ClockImage", false )
    else            
        -- DEBUG(L"Made it through 05 - timer")
        local timeLeft = GameData.CityScenarioData.timeLeft
        local text = TimeUtils.FormatTimeCondensedTruncate(timeLeft)
        LabelSetText( overallTimerString.."TimerValue", text )
        WindowSetShowing( overallTimerString.."ClockImage", true )
    end
    
    for objectiveIndex = 1, EA_Window_CityTracker.NUM_OBJECTIVES
    do
        for questIndex = 1, EA_Window_CityTracker.NUM_QUESTS
        do
            local questWindowString = "EA_Window_CityTrackerMainQuest"..questIndex
            
            if (DataUtils.activeObjectivesData == nil) or
               (DataUtils.activeObjectivesData[objectiveIndex] == nil) or
               (DataUtils.activeObjectivesData[objectiveIndex].Quest == nil) or
               (DataUtils.activeObjectivesData[objectiveIndex].Quest[questIndex] == nil)
            then
                LabelSetText( questWindowString.."TimerValue", L"" )      
                WindowSetShowing( questWindowString.."ClockImage", false )
            else
                -- Timer (only show timer at all if this is the first quest in the first objective)
                if( DataUtils.activeObjectivesData[objectiveIndex].Quest[questIndex].maxTimer ~= 0 and
                    DataUtils.activeObjectivesData[objectiveIndex].Quest[questIndex].maxTimer ~= nil )
                then
                    -- DEBUG(L"Made it through 05 - timer")
                    local timeLeft = DataUtils.activeObjectivesData[objectiveIndex].Quest[questIndex].timeLeft
                    local text = TimeUtils.FormatTimeCondensedTruncate(timeLeft)
                    LabelSetText( questWindowString.."TimerValue", text )
                    WindowSetShowing( questWindowString.."ClockImage", true )
                else            
                    LabelSetText( questWindowString.."TimerValue", L"" )      
                    WindowSetShowing( questWindowString.."ClockImage", false )
                end
            end
        end
    end
end


function EA_Window_CityTracker.UpdateQuestVisibility()
    if ( #DataUtils.activeObjectivesData == 0) or (PQData.currentState ~= PQData.STATE_CLEAR)
    then
        WindowSetShowing("EA_Window_CityTrackerMain", false)
    else
        WindowSetShowing("EA_Window_CityTrackerMain", true)
    end
end

function EA_Window_CityTracker.UpdateMainWindowSize()

    local height = 0
    local overviewHeight = 0
    local mainHeight = 0
    local captureHeight = 0
    
    if WindowGetShowing("EA_Window_CityTracker")
    then
        if WindowGetShowing("EA_Window_CityTrackerOverview")
        then
        
            local _, headingY = LabelGetTextDimensions("EA_Window_CityTrackerOverviewStatus")
            overviewHeight = overviewHeight + headingY + 10 -- 10 is the anchor offset from the top of the window to status

            local _, descriptionY = LabelGetTextDimensions("EA_Window_CityTrackerOverviewDescription")
            overviewHeight = overviewHeight + descriptionY + 10 -- 10 is the anchor offset from the status to the description

            -- Add a bit more empty space at the bottom of the sub window            
            overviewHeight = overviewHeight + 10
            
            WindowSetDimensions("EA_Window_CityTrackerOverview", EA_Window_CityTracker.WIDTH, overviewHeight)
        end
    
        height = height + 5 -- anchor offset between Overview and Main
                
        if WindowGetShowing("EA_Window_CityTrackerMain")
        then
            -- Main Window.
            for objective = 1, EA_Window_CityTracker.NUM_OBJECTIVES
            do
                -- Quests    
                for questIndex = 1, EA_Window_CityTracker.NUM_QUESTS
                do
                    mainHeight = mainHeight + 1 -- 1 is the anchor offset between the quest label and quest conditions
                                                -- on a EA_Window_PublicQuestTrackerQuestCluster
                   
                    -- Only update the window when it actually contains data
                    local questWindowHeight = 0
                    local questWindowName = "EA_Window_CityTrackerMainQuest"..questIndex
                    if( WindowGetShowing( questWindowName ) == true )
                    then
                        local _, questLabelY   = LabelGetTextDimensions( questWindowName.."Label" )
                        local conditionsHeight = EA_Window_CityTracker.UpdateConditionsSize( questWindowName.."Data" )
                        
                        questWindowHeight = questLabelY + conditionsHeight

                        WindowSetDimensions( questWindowName.."Data", EA_Window_CityTracker.WIDTH, conditionsHeight  )
                        WindowSetDimensions( questWindowName,         EA_Window_CityTracker.WIDTH, questWindowHeight ) 
                    else
                        WindowSetDimensions( questWindowName.."Data", EA_Window_CityTracker.WIDTH, 0 ) 
                        WindowSetDimensions( questWindowName,         EA_Window_CityTracker.WIDTH, 0 ) 
                    end
                    
                    mainHeight = mainHeight + questWindowHeight
                end
                
                -- Opt out window
                local _, optOutY = WindowGetDimensions( "EA_Window_CityTrackerMainOptOutButton" )
                mainHeight = mainHeight + optOutY + 8 -- 8 is the anchor offset between the quests and the opt out window

            end
        end
        
        WindowSetDimensions( "EA_Window_CityTrackerMain", EA_Window_CityTracker.WIDTH, mainHeight )
        
        height = height + 10 -- anchor offset between Main and Capture
        
        if WindowGetShowing("EA_Window_CityTrackerCapture")
        then
            captureHeight = EA_Window_CityTracker.CAPTURE_BAR_HEIGHT
        end
    end
    
    height = height + overviewHeight + mainHeight + captureHeight
    
    if (height < EA_Window_CityTracker.EMPTY_SIZE.y)
    then
        height = EA_Window_CityTracker.EMPTY_SIZE.y
    end
    WindowSetDimensions( "EA_Window_CityTracker", EA_Window_CityTracker.WIDTH, height )
    
end

function EA_Window_CityTracker.UpdateConditionsSize( dataWindow )
    -- Resize on Conditions
    local conditionHeight = 0
    for condition = 1, EA_Window_CityTracker.NUM_CONDITION_COUNTERS
    do
        local windowName = dataWindow.."Condition"..condition
        local nameWindowName = windowName.."Name"
        if( WindowGetShowing(windowName) == true )
        then
            local _, conditionTextHeight = LabelGetTextDimensions( nameWindowName )
            WindowSetDimensions( nameWindowName, EA_Window_CityTracker.CONDITION_NAME_WIDTH, conditionTextHeight ) 
            WindowSetDimensions( windowName,     EA_Window_CityTracker.WIDTH,                conditionTextHeight ) 
            conditionHeight = conditionHeight + conditionTextHeight
        else
            WindowSetDimensions( nameWindowName, EA_Window_CityTracker.CONDITION_NAME_WIDTH, 0 ) 
            WindowSetDimensions( windowName,     EA_Window_CityTracker.WIDTH,                0 ) 
        end
    end
    
    return conditionHeight
end

-- Updates the Quest & Condition Data for an Objective
function EA_Window_CityTracker.UpdateTracker()
    WindowSetShowing("EA_Window_CityTracker", GameData.Player.isInSiege)
   
    -- DEBUG(L"EA_Window_CityTracker.UpdateTracker()")
    local captureObjectiveFound = false
    local mainObjectiveFound    = false
    
    for objective, objectiveData in ipairs (DataUtils.activeObjectivesData)
    do
        if ( objectiveData.isBattlefieldObjective )
        then
            -- DEBUG(L"  updating battlefield objective (index "..objective)
            EA_Window_CityTracker.UpdateCaptureObjective( objective )
            captureObjectiveFound = true
        elseif ( objectiveData.isCityBoss )
        then
            EA_Window_CityTracker.mainObjective = objective
            -- DEBUG(L"  updating main boss objective (index "..objective)
            EA_Window_CityTracker.UpdateMainObjective( objective )
            mainObjectiveFound = true
        end
    end
    
    -- If no boss was found, search again for an overall
    if (not mainObjectiveFound)
    then
        for objective, objectiveData in ipairs (DataUtils.activeObjectivesData)
        do
            if ( objectiveData.isBattlefieldObjective ) or ( objectiveData.isCityBoss )
            then
                -- do nothing
            else
                -- DEBUG(L"  updating main overall objective (index "..objective)
                EA_Window_CityTracker.mainObjective = objective
                EA_Window_CityTracker.UpdateMainObjective( objective )
                mainObjectiveFound = true
            end
        end
    end

    WindowSetShowing("EA_Window_CityTrackerCapture", captureObjectiveFound)
    WindowSetShowing("EA_Window_CityTrackerMain",    mainObjectiveFound)
end


function EA_Window_CityTracker.UpdateStatus()

    -- No need to update the Tracker when not in use.
    if( GameData.Player.isInSiege == false )
    then    
        return
    end

    -- Set the status strings.
    local headingText = GetStringFromTable("RvRCityStrings", StringTables.RvRCity["HEADING_SIEGE_STATE_"..GameData.CityScenarioData.cityState] )
    LabelSetText( "EA_Window_CityTrackerOverviewStatus", headingText )

    
    -- first parameter city name, 2nd subboss PQ #1, 3rd subboss PQ #2, 4th attacking realm name, 5th defending realm name
    local parameters = { EA_Window_CityTracker.Strings.cityName, EA_Window_CityTracker.Strings.PQ1, EA_Window_CityTracker.Strings.PQ2, EA_Window_CityTracker.Strings.attackerRealmName, EA_Window_CityTracker.Strings.defenderRealmName }
    local descriptionText = GetStringFormatFromTable("RvRCityStrings", StringTables.RvRCity["DESCRIPTION_SIEGE_STATE_"..GameData.CityScenarioData.cityState..EA_Window_CityTracker.Strings.sideSuffix], parameters )
    LabelSetText( "EA_Window_CityTrackerOverviewDescription", descriptionText )
    
    -- Update the state timer
    EA_Window_CityTracker.UpdateQuestTimer()
    
    -- Resize the window to fit
    EA_Window_CityTracker.UpdateMainWindowSize()
end


function EA_Window_CityTracker.UpdateMainObjective( objective )
    local objectiveData = DataUtils.activeObjectivesData[objective]
    local objectiveWindowString = "EA_Window_CityTrackerMain"

    -- Quests
    for questIndex, questData in ipairs(objectiveData.Quest)
    do
        -- DEBUG(L"Made it through 02")
        local questWindowString = "EA_Window_CityTrackerMainQuest"..questIndex
        WindowSetShowing( questWindowString, true )
        --DEBUG(L"  Quest #"..questIndex..L" '"..GameData.ActiveObjectives[objective].Quest[questIndex].name..L"'")

        -- Name (ignore the objective's name in favor of the quests' name)
        local name = DataUtils.activeObjectivesData[objective].Quest[questIndex].name

        -- DEBUG(L"Made it through 04")
        LabelSetText(questWindowString.."Label", name )
        --DEBUG(L"  Quest #"..questIndex..L" '"..name..L"' with dimensions: x="..x..L", y="..y )

        -- set opt out flag
        ButtonSetPressedFlag( "EA_Window_CityTrackerMainOptOutButton", DataUtils.activeObjectivesData[objective].optedOutForLoot )

        -- Conditions
        for index, data in ipairs(questData.conditions)
        do
            local conditionName = data.name
            local curCounter    = data.curCounter
            local maxCounter    = data.maxCounter
            local isFailureCondition = data.failureCondition
            
            local nameLabel    = questWindowString.."DataCondition"..index.."Name"
            local counterLabel = questWindowString.."DataCondition"..index.."Counter"
            local typeImage    = questWindowString.."DataCondition"..index.."Type"

            -- Only show the conditions if conditionName is not empty
            if( conditionName ~= L"" )
            then
            -- DEBUG(L"Made it through 06 - conditions")
                
                if( maxCounter > 0 )
                then
                    LabelSetText( nameLabel, conditionName..L" - " )
                    LabelSetText( counterLabel, L""..curCounter..L"/"..maxCounter )

                    if( curCounter == maxCounter)
                    then
                        local completeColorForRealm = DefaultColor.RealmColors[questData.availableRealm]
                        LabelSetTextColor(counterLabel, completeColorForRealm.r, completeColorForRealm.g, completeColorForRealm.b )
                        LabelSetTextColor(nameLabel,    completeColorForRealm.r, completeColorForRealm.g, completeColorForRealm.b )
                    else
                        LabelSetTextColor(counterLabel, IncompleteCounterColor.r, IncompleteCounterColor.g, IncompleteCounterColor.b)
                        LabelSetTextColor(nameLabel,    IncompleteCounterColor.r, IncompleteCounterColor.g, IncompleteCounterColor.b)
                    end         
                    
                    --DEBUG(L"    Condition #"..condition..L": "..curCounter..L" of "..maxCounter..L" counters.")
                else
                    LabelSetText( nameLabel, conditionName )            
                    LabelSetText( counterLabel, L"" )
                end

                WindowSetShowing(questWindowString.."DataCondition"..index, true)
            else
                LabelSetText( nameLabel, L"" )
                LabelSetText( counterLabel, L"" )
                WindowSetShowing(questWindowString.."DataCondition"..index, false)
            end
            
            WindowSetShowing(typeImage, false)
        end
        
        for index = #questData.conditions + 1, EA_Window_CityTracker.NUM_CONDITION_COUNTERS
        do
            local nameLabel    = questWindowString.."DataCondition"..index.."Name"
            local counterLabel = questWindowString.."DataCondition"..index.."Counter"

            LabelSetText( nameLabel, L"" )
            LabelSetText( counterLabel, L"" )
            WindowSetShowing(questWindowString.."DataCondition"..index, false)
        end
        
    end
    
    for emptyQuestIndex = (#objectiveData.Quest + 1), EA_Window_CityTracker.NUM_QUESTS
    do
        local questWindowString = "EA_Window_CityTrackerMainQuest"..emptyQuestIndex
        WindowSetShowing( questWindowString, false )
    end
    
    EA_Window_CityTracker.UpdateMainWindowSize()

end

function EA_Window_CityTracker.UpdateControl()
    DataUtils.activeObjectivesData = GetActiveObjectivesData()

    EA_Window_CityTracker.UpdateCaptureObjective( GameData.ActiveObjectives.updatedObjectiveIndex )
end

function EA_Window_CityTracker.UpdateCaptureObjective( objective )
    -- DEBUG(L"EA_Window_CityTracker.UpdateCaptureObjective("..objective..L")")
    local objectiveData = DataUtils.activeObjectivesData[objective]
    local objectiveWindowString = "EA_Window_CityTrackerCapture"
    
    if (objectiveData == nil)
    then
        WindowSetShowing(objectiveWindowString, false)
    end
    
    -- name
    local nameLabel = objectiveWindowString.."Name"
    LabelSetText(nameLabel, objectiveData.name)
    
    -- flag
    local owner = TrackerUtils.GetFlagSliceForOwner(objectiveData.controllingRealm)
    DynamicImageSetTextureSlice(objectiveWindowString.."Owner", owner)
    
    -- capture bar
    local controlBarWindow = objectiveWindowString.."ControlBar"
    
    local controlFill = math.abs(objectiveData.curControlPoints)
    StatusBarSetCurrentValue( controlBarWindow, controlFill )
    
    local realm = GameData.Realm.NONE
    if ( objectiveData.curControlPoints < 0 )
    then
        realm = GameData.Realm.ORDER
    elseif ( objectiveData.curControlPoints > 0 )
    then
        realm = GameData.Realm.DESTRUCTION
    end
    
    local barColor = DefaultColor.RealmColors[realm]
    WindowSetTintColor( controlBarWindow, barColor.r, barColor.g, barColor.b )
    
    if (not WindowGetShowing(controlBarWindow))
    then
        WindowSetShowing(controlBarWindow, true)
    end

end


function EA_Window_CityTracker.MouseOverQuest()

    local objective  = EA_Window_CityTracker.mainObjective
    local questIndex = WindowGetId( SystemData.ActiveWindow.name )
    -- DEBUG(L"EA_Window_CityTracker.MouseOverQuest - ActiveObjective Index = "..objective..L", quest index = "..questIndex )

    if (DataUtils.activeObjectivesData[objective] == nil)
    then
        return
    elseif ( DataUtils.activeObjectivesData[objective].Quest[questIndex] == nil )
    then
        return
    end
    
    local objectiveData = DataUtils.activeObjectivesData[objective]
    local questData     = objectiveData.Quest[questIndex]

    Tooltips.CreateTextOnlyTooltip( SystemData.ActiveWindow.name )    
    local row = 1
    local column = 1

    -- Name
    local text = questData.name
    Tooltips.SetTooltipFont( row, column, "font_default_sub_heading", WindowUtils.FONT_DEFAULT_SUB_HEADING_LINESPACING  )
    Tooltips.SetTooltipText( row, column, text )
    Tooltips.SetTooltipColor( row, column, 255, 204, 102 )
    
    -- Complete Label
    column = column + 1 
    if( questData.complete == true )
    then
        text = GetString( StringTables.Default.LABEL_COMPLETE )         
        local completeColorForRealm = DefaultColor.RealmColors[questData.availableRealm]
        Tooltips.SetTooltipColor( row, column, completeColorForRealm.r, completeColorForRealm.g, completeColorForRealm.b)
        Tooltips.SetTooltipText( row, column, text )
    end
    row = row + 1
    column = 1
    
    -- Objective Label
    text = GetString( StringTables.Default.LABEL_PUBLIC_QUEST ) 
    -- Difficulty
    local difficultyText = TrackerUtils.GetDifficultyText( objectiveData.difficulty )
    text = text..L" ("..difficultyText..L")"
    Tooltips.SetTooltipText( row, column, text )
    Tooltips.SetTooltipColor( row, column, 100, 100, 100)
    
    row = row + 1
    
    -- Text
    local text = questData.desc
    Tooltips.SetTooltipText( row, column, text )
    row = row + 1
    column = 1
    
    -- Difficulty help
    text = TrackerUtils.GetDifficultyHelpText( objectiveData.difficulty )
    local color = TrackerUtils.GetDifficultyColor( objectiveData.difficulty )
    Tooltips.SetTooltipText( row, column, text )
    Tooltips.SetTooltipColor( row, column, color.r, color.g, color.b )

    Tooltips.Finalize()
    Tooltips.AnchorTooltip( Tooltips.ANCHOR_WINDOW_LEFT )   
end

function EA_Window_CityTracker.ToggleOptOut()
    
    local optedOutForLootRoll = ButtonGetPressedFlag( "EA_Window_CityTrackerMainOptOutButton" )
    TrackerUtils.OnMouseOverOptOut()
 
    local objective  = EA_Window_CityTracker.mainObjective
    if (DataUtils.activeObjectivesData[objective] )
    then
        local objectiveId = DataUtils.activeObjectivesData[objective].id
        LootRollOptOut(objectiveId, optedOutForLootRoll) 
    end
    
end

function EA_Window_CityTracker.OnQuestOptOut(index, optOut)

    if( index ~= EA_Window_CityTracker.mainObjective )
    then
        DisplayObjectiveOutOfRangeError(index)
        return -- This should never happen
    end
    
    ButtonSetPressedFlag( "EA_Window_CityTrackerMainOptOutButton", optOut )

    DataUtils.activeObjectivesData[index].optedOutForLoot = optOut
end

